package com.homegoods.orderinfodetail.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.databinding.InfoActivityShopDetailBinding;
import com.homegoods.orderinfodetail.presenter.ShopDetailPresenter;
import com.kedll.library.base.KRefreshListActivity;
import com.kedll.library.util.MyUtils;
import com.share.sharelibrary.share.ShareData;

import cn.jzvd.Jzvd;

/**
 * @author QiQi
 * @description 店铺详情
 */
@Route(path = ARouterPath.PATH_SHOP_DETAIL)
public class ShopDetailActivity extends KRefreshListActivity<InfoActivityShopDetailBinding, ShopDetailPresenter> {

    /**
     * 店铺ID
     */
    @Autowired(name = "shopSid")
    public String shopSid;

    @Override
    public int getLayout() {
        return R.layout.info_activity_shop_detail;
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        onClicked(mBinding.ibShare, v -> new ShareData.Builder()
                .setShareType(ShareData.SHARE_WEB)
                .setUrl("http://wwww.baidu.com")
                .builder().startShare());
    }

    @Override
    protected ShopDetailPresenter getPresenter() {
        return new ShopDetailPresenter(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        ARouter.getInstance().inject(this);
        mPresenter.getShop(shopSid);
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return MyUtils.getFlexBoxLayoutManager(this);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        list.addItemDecoration(new SpacesItemDecoration(2, 8));
        list.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
                if (manager instanceof FlexboxLayoutManager) {
                    FlexboxLayoutManager flexboxLayoutManager = (FlexboxLayoutManager) manager;
                    if (flexboxLayoutManager.findFirstVisibleItemPosition() == 3) {
                        Jzvd.goOnPlayOnPause();
                    }
                }
            }
        });

//        list.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
//            @Override
//            public void onChildViewAttachedToWindow(View view) {
//                Jzvd.onChildViewAttachedToWindow(view, R.id.jzvd_std);
//            }
//
//            @Override
//            public void onChildViewDetachedFromWindow(View view) {
//                Jzvd.onChildViewDetachedFromWindow(view);
//            }
//        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    /**
     * 设置RecycleView间的间距
     *
     * @author 11470
     */
    private final static class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        private int itemCount;
        private String TAG = SpacesItemDecoration.class.getName();

        SpacesItemDecoration(int itemCount, int space) {
            this.itemCount = itemCount;
            //屏幕宽度
            this.space = MyUtils.dp2px(space);

        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

            Log.i(TAG, "getItemOffsets: " + parent.getChildLayoutPosition(view));

            if (parent.getChildLayoutPosition(view) < 3) {
                outRect.left = 0;
                outRect.right = 0;
            } else {
                outRect.bottom = space;
                if ((parent.getChildLayoutPosition(view) - 3) % itemCount == 0) {
                    outRect.left = 0;
                    outRect.right = space / 2;
                } else if ((parent.getChildLayoutPosition(view) - 3) % itemCount == itemCount - 1) {
                    outRect.right = 0;
                    outRect.left = space / 2;
                } else {
                    outRect.right = space / 2;
                    outRect.left = space / 2;
                }
            }
        }
    }
}
